package com.carleasoft.mps.cms.business.repairs.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.repairs.bean.entity.RepairsGroup;
import com.carleasoft.mps.cms.business.repairs.bean.po.RepairsGroupAddOrEditPo;
import com.carleasoft.mps.cms.business.repairs.bean.po.RepairsGroupPageQueryPo;
import com.carleasoft.mps.cms.business.repairs.bean.po.SetDealPersonPo;
import com.carleasoft.mps.cms.business.repairs.constants.ExceptionRepairsEnum;
import com.carleasoft.mps.cms.business.repairs.dao.RepairsGroupMapper;
import com.carleasoft.mps.cms.business.repairs.service.RepairsGroupService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 维修组
 * @Author: fan
 */
@Service
public class RepairsGroupServiceImpl extends ServiceImpl<RepairsGroupMapper, RepairsGroup> implements RepairsGroupService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(RepairsGroupAddOrEditPo repairsGroupAddOrEditPo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        String personType = repairsGroupAddOrEditPo.getPersonType();
        List<String> strings = JSONObject.parseArray(personType,String.class);
        strings.stream().forEach(a->{
            Integer num = this.baseMapper.checkAddRepairsGroup(a, repairsGroupAddOrEditPo.getPersonCode());
            if (num>0 && a.equals("1")){
                throw new BusinessException(ExceptionRepairsEnum.BELONG_CAR);
            }
            if (num>0 && a.equals("2")){
                throw new BusinessException(ExceptionRepairsEnum.BELONG_EQUIP);
            }
            if (num>0 && a.equals("3")){
                throw new BusinessException(ExceptionRepairsEnum.BELONG_BARRACKS);
            }
        });
        RepairsGroup repairsGroup = new RepairsGroup();
        BeanUtils.copyProperties(repairsGroupAddOrEditPo,repairsGroup);
        repairsGroup.setRepairCode(UUID.randomUUID().toString().replaceAll("-",""));
        repairsGroup.setCreatePersonCode(user.getUserCode());
        repairsGroup.setCreatePersonName(user.getUserName());
        repairsGroup.setCreateTime(LocalDateTime.now());
        repairsGroup.setUpdatePersonCode(user.getUserCode());
        repairsGroup.setUpdatePersonName(user.getUserName());
        repairsGroup.setUpdateTime(LocalDateTime.now());
        repairsGroup.setIsCar(0);
        repairsGroup.setIsBarracks(0);
        repairsGroup.setIsEquip(0);
        repairsGroup.setVehicleMaintainNum(0);
        repairsGroup.setEquipMaintainNum(0);
        repairsGroup.setBarracksMaintainNum(0);
        return this.save(repairsGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(RepairsGroupAddOrEditPo repairsGroupAddOrEditPo) {
        IBeanUtil.paramVerify(repairsGroupAddOrEditPo,"repairCode");
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        LambdaQueryWrapper<RepairsGroup> queryWrapper = Wrappers.lambdaQuery(RepairsGroup.class).eq(RepairsGroup::getRepairCode, repairsGroupAddOrEditPo.getRepairCode());
        RepairsGroup one = this.getOne(queryWrapper);
        if (one!=null){
            if (one.getIsCar()==1 && !(repairsGroupAddOrEditPo.getPersonType().contains("1"))){
                throw new BusinessException(ExceptionRepairsEnum.CAR_DEAL_PERSON);
            }
            if (one.getIsEquip()==1 && !(repairsGroupAddOrEditPo.getPersonType().contains("2"))){
                throw new BusinessException(ExceptionRepairsEnum.EQUIP_DEAL_PERSON);
            }
            if (one.getIsBarracks()==1 && !(repairsGroupAddOrEditPo.getPersonType().contains("3"))){
                throw new BusinessException(ExceptionRepairsEnum.BARRACKS_DEAL_PERSON);
            }
        }
        RepairsGroup repairsGroup = new RepairsGroup();
        BeanUtils.copyProperties(repairsGroupAddOrEditPo,repairsGroup);
        repairsGroup.setUpdatePersonCode(user.getUserCode());
        repairsGroup.setUpdatePersonName(user.getUserName());
        repairsGroup.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<RepairsGroup> eq = Wrappers.lambdaQuery(RepairsGroup.class).eq(RepairsGroup::getRepairCode, repairsGroupAddOrEditPo.getRepairCode());
        return this.update(repairsGroup,eq);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(RepairsGroup repairsGroup) {
        IBeanUtil.paramVerify(repairsGroup,"repairCode");
        RepairsGroup one = this.getOne(new QueryWrapper<RepairsGroup>().eq("REPAIR_CODE", repairsGroup.getRepairCode()));
        if (one.getIsCar()==1){
            throw new BusinessException(ExceptionRepairsEnum.IS_CAR);
        }
        if (one.getIsEquip()==1){
            throw new BusinessException(ExceptionRepairsEnum.IS_CAR);
        }
        if (one.getIsBarracks()==1){
            throw new BusinessException(ExceptionRepairsEnum.IS_CAR);
        }
        LambdaQueryWrapper<RepairsGroup> eq = Wrappers.lambdaQuery(RepairsGroup.class).eq(RepairsGroup::getRepairCode, repairsGroup.getRepairCode());
        return this.remove(eq);
    }

    @Override
    public Page<RepairsGroup> pageQuery(RepairsGroupPageQueryPo repairsGroupPageQueryPo) {
        Page page = new Page(repairsGroupPageQueryPo.getCurrent(),repairsGroupPageQueryPo.getSize());
//        LambdaQueryWrapper<RepairsGroup> eq = Wrappers.lambdaQuery(RepairsGroup.class).orderByDesc(RepairsGroup::getUpdateTime);
//        if (StringUtils.isNotBlank(repairsGroupPageQueryPo.getPersonName())){
//            eq.like(RepairsGroup::getPersonName,repairsGroupPageQueryPo.getPersonName());
//        }
//        if (StringUtils.isNotBlank(repairsGroupPageQueryPo.getMaintainDomain())){
//            eq.like(RepairsGroup::getMaintainDomain,repairsGroupPageQueryPo.getMaintainDomain());
//        }
        return this.baseMapper.pageQuery(page,repairsGroupPageQueryPo);
    }

    @Override
    public List<RepairsGroup> selList( RepairsGroup repairsGroup) {
        return this.baseMapper.list(repairsGroup);
    }

    @Override
    public RepairsGroup detail(RepairsGroup repairsGroup) {
        IBeanUtil.paramVerify(repairsGroup,"repairCode");
//        LambdaQueryWrapper<RepairsGroup> eq = Wrappers.lambdaQuery(RepairsGroup.class).eq(RepairsGroup::getRepairCode, repairsGroup.getRepairCode());
        return this.baseMapper.detail(repairsGroup.getRepairCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setDealPerson(SetDealPersonPo setDealPersonPo) {
        UpdateWrapper<RepairsGroup> up0 = new UpdateWrapper<>();
        up0.set("IS_CAR",1);
        up0.eq("REPAIR_CODE",setDealPersonPo.getCarCode());
        UpdateWrapper<RepairsGroup> up1 = new UpdateWrapper<>();
        up1.set("IS_EQUIP",1);
        up1.eq("REPAIR_CODE",setDealPersonPo.getEquipCode());
        UpdateWrapper<RepairsGroup> up2 = new UpdateWrapper<>();
        up2.set("IS_BARRACKS",1);
        up2.eq("REPAIR_CODE",setDealPersonPo.getBarracksCode());
        UpdateWrapper<RepairsGroup> up3 = new UpdateWrapper<>();
        up3.set("IS_CAR",0);//把原来的设为普通维修员
        up3.set("IS_EQUIP",0);
        up3.set("IS_BARRACKS",0);
        up3.eq("IS_CAR",1).or().eq("IS_EQUIP",1).or().eq("IS_BARRACKS",1);
        this.update(up3);
        this.update(up0);
        this.update(up1);
        this.update(up2);
        return true;
    }

    @Override
    public SetDealPersonPo selDealPerson() {
        SetDealPersonPo setDealPersonPo = new SetDealPersonPo();
        RepairsGroup is_car = this.getOne(new QueryWrapper<RepairsGroup>().eq("IS_CAR", 1));
        RepairsGroup is_equip = this.getOne(new QueryWrapper<RepairsGroup>().eq("IS_EQUIP", 1));
        RepairsGroup is_barracks = this.getOne(new QueryWrapper<RepairsGroup>().eq("IS_BARRACKS", 1));
        if (is_car!=null){
            setDealPersonPo.setCarCode(is_car.getRepairCode());
        }
        if (is_equip!=null){
            setDealPersonPo.setEquipCode(is_equip.getRepairCode());
        }
        if (is_barracks!=null){
            setDealPersonPo.setBarracksCode(is_barracks.getRepairCode());
        }
        return setDealPersonPo;
    }
}
